package cloud.tianai.mate.captcha.generator.common.util;

import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 天爱有情
 * @date 2023/4/24 10:00
 * @Description 图片随机乱序排序
 */
public class ImageShuffleUtils {

    public static ShuffleImageResponse shuffle(BufferedImage sourceImage, int xNum, int yNum) {
        int width = sourceImage.getWidth();
        int height = sourceImage.getHeight();
        int blockSize = xNum * yNum;
        int xBlock = width / xNum;
        int yBlock = height / yNum;
        List<Block> gridBlockPositions = new ArrayList<>(blockSize);
        int index = 0;
        for (int i = 0; i < yNum; i++) {
            for (int o = 0; o < xNum; o++) {
                int startX = o * xBlock;
                int startY = i * yBlock;
                int endX = (o + 1) * xBlock;
                int endY = (i + 1) * yBlock;
                gridBlockPositions.add(new Block(index++, (i * xNum) + o, startX, startY, endX, endY));
            }
        }
        // 乱序
        List<Block> shuffledGridBlockPositions = new ArrayList<>(gridBlockPositions);
        Collections.shuffle(shuffledGridBlockPositions);
        BufferedImage suffleImage = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), sourceImage.getType());
        Graphics2D graphics = suffleImage.createGraphics();
        for (int i = 0; i < shuffledGridBlockPositions.size(); i++) {
            Block block = shuffledGridBlockPositions.get(i);
            Block block2 = gridBlockPositions.get(i);
            graphics.drawImage(sourceImage, block.getStartX(), block.getStartY(), block.getEndX(), block.getEndY(),
                    block2.getStartX(), block2.getStartY(), block2.endX, block2.endY, null);
        }

        graphics.dispose();

        List<Integer> posIndexList = shuffledGridBlockPositions.stream().map(Block::getIndex).collect(Collectors.toList());

        ShuffleImageResponse response = new ShuffleImageResponse();
        response.setImage(suffleImage);
        response.setX(xNum);
        response.setY(yNum);
        response.setPos(posIndexList);
        return response;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Block {
        private int index;
        private int pos;
        private int startX;
        private int startY;
        private int endX;
        private int endY;
    }

    @Data
    public static class ShuffleImageResponse {
        private BufferedImage image;
        private int x;
        private int y;
        private List<Integer> pos;
    }


    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Thinkpad\\Desktop\\captcha\\bgimages\\1.jpeg");
        BufferedImage bufferedImage = CaptchaImageUtils.wrapFile2BufferedImage(fileInputStream);
        fileInputStream.close();
        ShuffleImageResponse shuffle = shuffle(bufferedImage, 2, 3);

        BufferedImage image = shuffle.getImage();
        FileOutputStream out = new FileOutputStream("C:\\Users\\Thinkpad\\Desktop\\captcha\\temp\\1.jpeg");
        ImageIO.write(image, "jpeg", out);
        out.close();

        System.out.println(shuffle.getPos());
    }
}
